import datetime
import pathlib
import vectorbt  # 执行 pip show vectorbt 可以找到它的 Home-page
import vectorbt.indicators.factory  # https://vectorbt.dev/
import vectorbt.utils.figure
from vnpy.trader.object import (
    Interval,
    Exchange,
    BarData,
    HistoryRequest,
)
from vnpy_zxtools.sundries.example import load_data


def example_1():
    """
    https://vectorbt.dev/#example
    """
    dd_beg = datetime.date(year=2025, month=1, day=2)
    dd_end = datetime.date(year=2025, month=3, day=6)

    dt_beg = datetime.datetime.combine(date=dd_beg, time=datetime.time(hour=19)) - datetime.timedelta(days={0: 3, 5: 1, 6: 2}.get(dd_beg.weekday(), 1))
    dt_end = datetime.datetime.combine(date=dd_end, time=datetime.time(hour=17))

    df = load_data(
        exchange=Exchange.SHFE,
        symbol="agL8",
        interval=Interval.MINUTE05,
        start=dt_beg - datetime.timedelta(days=30),
        end=dt_end,
    )

    fast_ma = vectorbt.MA.run(df["close"], window=10, short_name='fast')
    slow_ma = vectorbt.MA.run(df["close"], window=20, short_name='slow')
    entries = fast_ma.ma_crossed_above(slow_ma)
    exits = fast_ma.ma_crossed_below(slow_ma)
    pf = vectorbt.Portfolio.from_signals(df["close"], entries, exits)

    print(pf.total_return())


def example_2():
    """"""
    dd_beg = datetime.date(year=2025, month=1, day=2)
    dd_end = datetime.date(year=2025, month=3, day=6)

    dt_beg = datetime.datetime.combine(date=dd_beg, time=datetime.time(hour=19)) - datetime.timedelta(days={0: 3, 5: 1, 6: 2}.get(dd_beg.weekday(), 1))
    dt_end = datetime.datetime.combine(date=dd_end, time=datetime.time(hour=17))

    df = load_data(
        exchange=Exchange.SHFE,
        symbol="agL8",
        interval=Interval.MINUTE05,
        start=dt_beg - datetime.timedelta(days=30),
        end=dt_end,
    )

    fast_ma = vectorbt.MA.run(df["close"], window=[10, 20], short_name='fast')
    slow_ma = vectorbt.MA.run(df["close"], window=[30, 30], short_name='slow')
    entries = fast_ma.ma_crossed_above(slow_ma)
    exits = fast_ma.ma_crossed_below(slow_ma)
    pf = vectorbt.Portfolio.from_signals(df["close"], entries, exits)

    print(pf.total_return())


def example_3():
    """
    https://vectorbt.dev/getting-started/features/#modeling
    """
    dd_beg = datetime.date(year=2025, month=1, day=2)
    dd_end = datetime.date(year=2025, month=3, day=6)

    dt_beg = datetime.datetime.combine(date=dd_beg, time=datetime.time(hour=19)) - datetime.timedelta(days={0: 3, 5: 1, 6: 2}.get(dd_beg.weekday(), 1))
    dt_end = datetime.datetime.combine(date=dd_end, time=datetime.time(hour=17))

    df = load_data(
        exchange=Exchange.SHFE,
        symbol="agL8",
        interval=Interval.MINUTE05,
        start=dt_beg - datetime.timedelta(days=30),
        end=dt_end,
    )

    fast_ma = vectorbt.MA.run(df["close"], window=50, short_name='fast')
    slow_ma = vectorbt.MA.run(df["close"], window=200, short_name='slow')
    entries = fast_ma.ma_crossed_above(slow_ma)
    exits = fast_ma.ma_crossed_below(slow_ma)
    pf = vectorbt.Portfolio.from_signals(df["close"], entries, exits, fees=0.005)

    fig = df["close"].vbt.plot(trace_kwargs=dict(name='Close'))
    assert isinstance(fig, vectorbt.utils.figure.FigureWidget)
    fast_ma.ma.vbt.plot(trace_kwargs=dict(name='Fast MA'), fig=fig)
    slow_ma.ma.vbt.plot(trace_kwargs=dict(name='Slow MA'), fig=fig)
    pf.positions.plot(close_trace_kwargs=dict(visible=False), fig=fig)
    fig.write_html(file=__file__ + ".html")


if __name__ == "__main__":
    example_3()
